package com.chen.web;

import com.alibaba.fastjson.JSON;
import com.chen.dao.UserDao;
import com.chen.dao.impl.UserDaoImpl;
import com.chen.entity.JSONBean;
import com.chen.entity.User;
import com.chen.util.MD5Util;
import com.chen.util.UUIDUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/user/*")
public class UserServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String baseURL = request.getContextPath(); //获取项目基准路径
        String uri = request.getRequestURI();//截取项目二级路径
        String path = uri.substring(uri.lastIndexOf("/") + 1);
        UserDao userDao = new UserDaoImpl();
        JSONBean jsonBean = new JSONBean();
        HttpSession session = request.getSession();
        if("user_login.do".equals(path)){
            String name = request.getParameter("name");
            String password = request.getParameter("password");
            String md5pwd = MD5Util.MD5(password);
            User user = userDao.login(name);
            if(null==user){
                jsonBean.setFail("用户名不存在");
            }else{
                if(!user.getUser_password().equals(md5pwd)){
                    jsonBean.setFail("密码错误");
                }else {
                    session.setAttribute("user_id",user.getUser_id());
                    jsonBean.setSuccess("登陆成功");
                }
            }
            response.setContentType("application/json;charset=utf-8");
            PrintWriter out = response.getWriter(); //获取输出
            String json = JSON.toJSONString(jsonBean);
            out.print(json);
            out.close();
        }else if("user_register.do".equals(path)){
            String uuid = UUIDUtils.getUUID();
            //获取用户名
            String username = request.getParameter("username");
            //获取手机号
            String phone = request.getParameter("phone");
            //获取密码
            String password = request.getParameter("password");
            String md5pwd = MD5Util.MD5(password);
            //获取确认密码
            String affirm_password = request.getParameter("affirm_password");
            //获取验证码
            String code = request.getParameter("code");
            //用户初始积分 integral-积分
            int integral = 0;
            //用户初始余额
            double money = 0;
            String piccode = session.getAttribute("piccode").toString();
            User user = new User();
            user.setUser_id(uuid);
            user.setUser_name(username);
            user.setUser_phone(phone);
            user.setUser_password(md5pwd);
            user.setUser_point(integral);
            user.setUser_money(money);
            if(code.equals(piccode)){
                if(password.equals(affirm_password)){
                    int result = userDao.save(user);
                    if(result>0){
                        jsonBean.setSuccess("注册成功");
                    }else{
                        jsonBean.setFail("注册失败");
                    }
                }else{
                    jsonBean.setFail("请确保两次密码输入一致");
                }
            }else{
               jsonBean.setFail("验证码输入错误");
            }
            response.setContentType("application/json;charset=utf-8");
            PrintWriter out = response.getWriter(); //获取输出
            String json = JSON.toJSONString(jsonBean);
            out.print(json);
            out.close();

        }


    }



}
